﻿/*
 * This file is part of CommunitySettlers.
 *
 * CommunitySettlers is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * CommunitySettlers is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with CommunitySettlers.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Author: Christoph Husse
 * 
 * Also checkout our homepage: http://communitysettlers.codeplex.com/
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
using System.Data.Objects;
using System.Data;
using System.IO;
using System.Diagnostics;


namespace SettlersEngine
{
    internal static class Log
    {
        private static Stopwatch m_Stopwatch = new Stopwatch();
        private static Object m_MessageBufferLock = new Object();
        private static String m_MessageBuffer = "";

        static Log()
        {
            m_Stopwatch.Start();
        }

        internal static String ClearAndFetchMessageBuffer()
        {
            lock (m_MessageBufferLock)
            {
                String backup = m_MessageBuffer;

                m_MessageBuffer = "";

                return backup;
            }
        }

        internal static String GetDateFileName()
        {
            lock (m_MessageBufferLock)
            {
                String result = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");

                return result + "_" + m_Stopwatch.ElapsedMilliseconds;
            }
        }

        internal static void LogModalException(String inMessage, Exception e)
        {
            LogModalException("Error...", inMessage, e);
        }

        internal static void LogModalException(String inTitle, String inMessage, Exception e)
        {
            LogUIException(e);

            MessageBox.Show(inTitle, inMessage + "\n\n" + "(See Logs for details)", MessageBoxButton.OK, MessageBoxImage.Error);
        }

        internal static void LogUIException(Exception e)
        {
            LogUIException(null, e);
        }

        internal static void LogUIException(
            String InOptFileName,
            Exception e)
        {
            lock (m_MessageBufferLock)
            {
                String Message;

                if (InOptFileName != null)
                {
                    Message =
                        "###############################################################\n" +
                        "[EXCEPTION, " + DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss") + "]: \n" +
                        "---------------------------------------------------------------\n" +
                        InOptFileName + "\n" +
                        "---------------------------------------------------------------\n" +
                        "     =>\"" + e.ToString() + "\".\n" +
                        "---------------------------------------------------------------\n\n\n";
                }
                else
                {
                    Message =
                        "###############################################################\n" +
                        "[EXCEPTION, " + DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss") + "]: \n" +
                        "---------------------------------------------------------------\n" +
                        "     =>\"" + e.ToString() + "\".\n" +
                        "---------------------------------------------------------------\n\n\n";
                }

                m_MessageBuffer += Message;
            }
        }

        internal static void LogException(Exception e)
        {
            LogException(null, e);
        }

        internal static void LogException(
            String InOptFileName,
            Exception e)
        {
            lock (m_MessageBufferLock)
            {
                String Message;

                if (InOptFileName != null)
                {
                    Message =
                        "###############################################################\n" +
                        "[EXCEPTION, " + DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss") + "]: \n" +
                        "---------------------------------------------------------------\n" +
                        InOptFileName + "\n" +
                        "---------------------------------------------------------------\n" +
                        "     =>\"" + e.ToString() + "\".\n" +
                        "---------------------------------------------------------------\n\n\n";
                }
                else
                {
                    Message =
                        "###############################################################\n" +
                        "[EXCEPTION, " + DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss") + "]: \n" +
                        "---------------------------------------------------------------\n" +
                        "     =>\"" + e.ToString() + "\".\n" +
                        "---------------------------------------------------------------\n\n\n";
                }

                LogMessage(e.Message, Message);
            }
        }

        internal static void LogMessage(String inShortMessage, String inFullDetails)
        {
            lock (m_MessageBufferLock)
            {
                m_MessageBuffer += "[" + DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss") + "]: \n" + inShortMessage + "\n";

                if (inFullDetails != null)
                {
                    StreamWriter Writer = File.AppendText("exceptions.log");

                    using (Writer)
                    {
                        Writer.Write(inFullDetails);

                        Writer.Flush();
                    }
                }
            }
        }
    }
}
